﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using uMES.LeanManufacturing.ParameterDTO;
using System.Data;

namespace uMES.LeanManufacturing.Common
{
    public class clsParseQuery
    {
        public string BuildQuery(List<uMESQueryParameter> lstQuery)
        {
            try
                {
                if (lstQuery != null)
                {
                    if (lstQuery.Count > 0)
                    {
                        string strSQL = "";
                        string strFileName, strFilterValue, strFieldValue, strDataType;
                        foreach (uMESQueryParameter oQuery in lstQuery)
                        {
                            strDataType = oQuery.DataType;
                            strFileName = oQuery.FieldName ;
                            strFilterValue = oQuery.FilterValue  ;
                            strFieldValue = oQuery.FieldValue ;
                            switch (strDataType)
                            { 
                                case "string":
                                    strSQL += " AND " + strFileName + " LIKE '%" + strFieldValue + "%' ";
                                    break;
                                case "date":
                                    strSQL += " AND " + strFileName + " " + strFilterValue + " To_Date('" + strFieldValue + "','RRRR-MM-DD') ";
                                    break;
                                case "time":
                                    strSQL += " AND " + strFileName + " " + strFilterValue + " To_Date('" + strFieldValue + "','RRRR-MM-DD HH24:MI:SS') ";
                                    break;
                                default:
                                    strSQL += " AND " + strFileName + " " + strFilterValue + " '" + strFieldValue + "' ";
                                    break;
                            }

                        }

                        return strSQL;
                    }
                    else
                    {
                        return "";
                    }
                }
                else
                {
                    return "";
                }
            }
              catch (Exception ex)
              {
                     throw ex;
              }
        }

        public string BuildQueryDataTable(List<uMESQueryParameter> lstQuery)
        {
            try
            {
                if (lstQuery != null)
                {
                    if (lstQuery.Count > 0)
                    {
                        string strSQL = "";
                        string strFileName, strFilterValue, strFieldValue, strDataType;
                        foreach (uMESQueryParameter oQuery in lstQuery)
                        {
                            strDataType = oQuery.DataType;
                            strFileName = oQuery.FieldName;
                            strFilterValue = oQuery.FilterValue;
                            strFieldValue = oQuery.FieldValue;
                            switch (strDataType)
                            {
                                case "string":
                                    strSQL += " AND " + strFileName + " LIKE '%" + strFieldValue + "%' ";
                                    break;
                                case "date":
                                    strSQL += " AND " + strFileName + " " + strFilterValue + " #" + strFieldValue + "# ";
                                    break;
                                case "time":
                                    strSQL += " AND " + strFileName + " " + strFilterValue + " #" + strFieldValue + "# ";
                                    break;
                                default:
                                    strSQL += " AND " + strFileName + " " + strFilterValue + " '" + strFieldValue + "' ";
                                    break;
                            }

                        }

                        return strSQL;
                    }
                    else
                    {
                        return "";
                    }
                }
                else
                {
                    return "";
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        public DataTable DataFilter(DataTable dt, string strFilter)
        {
            try
            {
                DataRow[] arrayDR = dt.Select(strFilter);
                DataTable dtNew = new DataTable();
                dtNew = dt.Clone();
                dtNew.Clear();
                foreach (DataRow dr in arrayDR)
                {
                    dtNew.Rows.Add(dr.ItemArray);
                }
                return dtNew;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public DataTable DataFilter(DataTable dt, string strFilter,string strOrder)
        {
            try
            {
                DataRow[] arrayDR = dt.Select(strFilter,strOrder);
                DataTable dtNew = new DataTable();
                dtNew = dt.Clone();
                dtNew.Clear();
                foreach (DataRow dr in arrayDR)
                {
                    dtNew.Rows.Add(dr.ItemArray);
                }
                return dtNew;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
